package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.CreditCard;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class CreditCardDAO extends AbstractDao<CreditCard>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_credit_card";
	public static final String SELECT_SQL = "select * from oce_credit_card where creditCardId = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_credit_card (creditCardId,card_type,card_number,expiration_time,card_security_code) VALUES (?,?,?,?,?)";
	public static final String UPDATE_SQL = "UPDATE oce_credit_card SET card_type = ?, card_number = ?, expiration_time = ?, card_security_code = ? WHERE creditCardId = ? ";
	
	public CreditCard create(CreditCard obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			long id = 0l;
			ps.setLong(1,id);
			ps.setString(2, obj.getCardType());
			ps.setString(3, obj.getCardNumber());
			ps.setDate(4, obj.getExpirationTime());
			ps.setString(5, obj.getSecurityCode());				
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public CreditCard update(CreditCard obj){
		try{
			
			if(obj == null || obj.getId() == null || obj.getId().equals(""))
				return obj;
						
			PreparedStatement ps = con.prepareStatement(UPDATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(1, obj.getCardType());
			ps.setString(2, obj.getCardNumber());
			ps.setDate(3, obj.getExpirationTime());
			ps.setString(4, obj.getSecurityCode());
			ps.setString(5, obj.getId());
						
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public CreditCard find(long id){
		List<CreditCard> cards = new ArrayList<CreditCard>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			cards = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return cards.size()== 0 ? null : cards.get(0);
	}
	
	public List<CreditCard> findAll(){
		List<CreditCard> cards = new ArrayList<CreditCard>();
		try{
			cards = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			cards = null;
		}
		return cards;
	}
	
	public void delete(CreditCard obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<CreditCard> convert(ResultSet rs) throws SQLException {
		List<CreditCard> cards = new ArrayList<CreditCard>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String cardType = rs.getString(2);
	        String cardNumber = rs.getString(3);
	        Date expiration_time = rs.getDate(4);
	        String securityCode = rs.getString(5);
	        cards.add(new CreditCard(id,cardType,cardNumber,expiration_time,securityCode));
	    }
		rs.close();
		return cards;
	}
}
